package com.wissen.temperatureapp.client.widgets;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
//import com.google.gwt.user.client.ui.HasHorizontalAlignment;
//import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.widgetideas.client.SliderBar;
import com.wissen.temperatureapp.client.controller.TempController;
import com.wissen.temperatureapp.client.observers.TemperatureObserver;

/**
 * Class represents a slider widget. Provides slider UI for user.
 * 
 * @author Mayur Birari
 * 
 */
public class SliderBarWidget extends Composite implements TemperatureObserver {

    private HorizontalPanel sliderbarPanel;

    private SliderBar sliderbar;
    
    private FlexTable      sliderbarTable;

    public SliderBarWidget(){}
    public SliderBarWidget(Double value) {
        init();

       sliderbarPanel.add(sliderbarTable);
        initWidget(sliderbarPanel);
        sliderbar.setCurrentValue(value);
        TempController.getInstance().addTempObserver(this);
    }

    private void init() {
    	sliderbarPanel = new HorizontalPanel();        
    	sliderbar = new SliderBar(0.0, 100.0);
    	  sliderbar.setStepSize(5.0);
    	  sliderbar.setCurrentValue(50.0);
    	  sliderbar.setNumTicks(10);
    	  sliderbar.setNumLabels(5);
    	  sliderbar.setWidth("700%");
        //creating table
    	sliderbarTable = new FlexTable();
    	sliderbarTable.setText(0, 0, "Slider Bar : ");
    	sliderbarTable.setWidget(1, 0,sliderbar);
    	//sliderbarTable.getFlexCellFormatter().setColSpan(2, 0, 2);
    	//sliderbarTable.getFlexCellFormatter().setAlignment(3, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE);
    }
    
    
    public void notifyConvertFailed(String errorMessage) {
        Window.alert("Conversion fail: " + errorMessage);
    }
    
    public void notifyConvertSucceeded(Double fvalue) {
    	sliderbar.setCurrentValue(fvalue);
    }
}
//http://code.google.com/docreader/#p=google-web-toolkit-incubator&s=google-web-toolkit-incubator&t=SliderBar